﻿using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Identity.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IM.Easy.Core.Cache
{
    public class CacheManager
    {
        private static ICache _cacheInstance;
        private static readonly object LockObj = new();

        // 获取缓存实例
        public static ICache Instance
        {
            get
            {
                if (_cacheInstance == null)
                {
                    throw new InvalidOperationException("请先初始化 CacheManager。使用 CacheManager.Initialize 方法初始化缓存类型。");
                }
                return _cacheInstance;
            }
        }

        /// <summary>
        /// 初始化缓存管理器，配置缓存类型。
        /// </summary>
        /// <param name="cacheOptionsAction">缓存实例创建委托</param>
        public static void Initialize(Action<CacheOption> cacheOptionsAction)
        {
            var cacheOptions = new CacheOption();
            cacheOptionsAction(cacheOptions);  // 配置 CacheOption 实例
            Initialize(cacheOptions);
        }

        public static void Initialize(CacheOption cacheOptions)
        {
            if (_cacheInstance != null)
            {
                throw new InvalidOperationException("CacheManager 已经初始化，不能重复初始化。");
            }
            lock (LockObj)
            {
                if (_cacheInstance == null)
                {
                    // 根据 CacheOption 配置初始化缓存实例
                    switch (cacheOptions.CacheType)
                    {
                        case CacheType.Redis:
                            _cacheInstance = new RedisCacheProvider(cacheOptions.ConnectionString);
                            break;

                        case CacheType.Memory:
                            // 使用 CacheSize 设置缓存大小限制
                            var memoryCacheOptions = new MemoryCacheOptions();
                            var memoryCache = new MemoryCache(memoryCacheOptions);  // 创建内存缓存实例
                            _cacheInstance = new MemoryCacheProvider(memoryCache);  // 初始化缓存提供者
                            break;

                        default:
                            throw new InvalidOperationException($"不支持的缓存类型: {cacheOptions.CacheType}");
                    }
                }
            }
        }

        /// <summary>
        /// 设置缓存值
        /// </summary>
        public static void Set(string key, object value, TimeSpan? absoluteExpirationRelativeToNow = null)
        {
            Instance.Set(key, value, absoluteExpirationRelativeToNow);
        }

        /// <summary>
        /// 获取缓存值
        /// </summary>
        public static T Get<T>(string key)
        {
            return Instance.Get<T>(key);
        }

        public static string Get(string key)
        {
            return Get<string>(key);
        }

        /// <summary>
        /// 移除缓存项
        /// </summary>
        public static void Remove(string key)
        {
            Instance.Remove(key);
        }

        /// <summary>
        /// 根据正则删除
        /// </summary>
        /// <param name="pattern"></param>
        public static void RemoveByPattern(string pattern)
        {
            Instance.RemoveByPattern(pattern);
        }

        /// <summary>
        /// 检查缓存项是否存在
        /// </summary>
        public static bool Exists(string key)
        {
            return Instance.Exists(key);
        }

        /// <summary>
        /// 清空所有缓存项
        /// </summary>
        public static void Clear()
        {
            Instance.Clear();
        }

        /// <summary>
        /// 获取所有缓存项的键值对
        /// </summary>
        public static IDictionary<string, string> GetAll()
        {
            return Instance.GetAll();
        }
    }

}
